/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.panomedic.kernel.controllers;

import com.panomedic.Constants;
import com.panomedic.colors.ColorConstants;
import com.panomedic.core.Photo;
import com.panomedic.core.Photos;
import com.panomedic.gui.Frame;
import com.panomedic.gui.LUTDialog;
import com.panomedic.gui.ToolBar;
import com.panomedic.kernel.VisualHandler;
import com.panomedic.log4j.Logger;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.prefs.Preferences;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Yare
 */
public class ToolBarListener implements ActionListener {

    static Logger logger = new Logger();
    private Preferences prefs;
    private ToolBar toolBar;

    public ToolBarListener(ToolBar toolBar) {
        prefs = Preferences.userRoot().node(Constants.defNodePath);
        this.toolBar = toolBar;
    }

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == toolBar.getShowResult()) {
            Photo photo = Photos.getResult();
            if (photo != null) {
                VisualHandler.getInstance().getFrame().setPhotoView(photo);
            } else {
                VisualHandler.getInstance().getFrame().setStatus("No result made.");
            }
        } else if (e.getSource() == toolBar.getProcess()) {
            if (Photos.getInstance().isEmpty()) {
                JOptionPane.showMessageDialog((JFrame) VisualHandler.getInstance().getFrame(),
                        "No photos loaded.",
                        "Warning",
                        JOptionPane.WARNING_MESSAGE);
                return;
            }
            System.err.println("method " + prefs.getInt(Constants.METHOD_CHOICE, Constants.METHOD_LUT));
            switch (prefs.getInt(Constants.METHOD_CHOICE, Constants.METHOD_LUT)) {
                case (Constants.METHOD_LUT):
                    Photos.processLevelMapping();
                    break;
                case (Constants.METHOD_STRETCHING):
                    Photos.processHistCalibration();
                    break;
            }
            VisualHandler.getInstance().getFrame().setPhotoView(Photos.getResult());
        } else if (e.getSource() == toolBar.getShowLut()) {
            int csType = prefs.getInt(Constants.CS_TYPE, ColorConstants.HSV);
            LUTDialog lutDialog = new LUTDialog((java.awt.Frame) (Frame) VisualHandler.getInstance().getFrame(), new String("Lookup Table - " + ColorConstants.getName(csType)), Photos.getLut());
        }
    }
}
